local skill = fk.CreateSkill {
  name = "3s__wangfu",
}

Fk:loadTranslationTable{
  ["3s__wangfu"] = "望伏",
  [":3s__wangfu"] = "当你指定或成为伤害牌的唯一目标后，目标须选择一项：1.回复1点体力，令此牌多结算一次；2.失去1点体力，此牌少结算一次。若其未横置，你可令其横置，然后其改为执行其选择的另一项。",
  ["$3s__wangfu1"] = "伏兵四起，望风而逃！",
  ["$3s__wangfu2"] = "设伏以待，敌必自乱！",
  ["#3s__wangfu-choose"] = "望伏：请选择一项",
  ["3s__wangfu_choice1"] = "回复1点体力，令此牌多结算一次",
  ["3s__wangfu_choice2"] = "失去1点体力，此牌少结算一次",
  ["#wangfu_choice"] = "%from 选择了：%arg",
  ["#3s__wangfu-chain"] = "望伏：是否令 %dest 横置并改为执行另一项？",
}

skill:addEffect(fk.AfterCardTargetDeclared, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skill.name) and
      data:isOnlyTarget(data.tos[1]) and
      data.card.is_damage_card and
      (target == player or data.tos[1] == player)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = data:getAllTargets()[1]
    if to.dead then return end
    -- 让目标选择
    local all_choices = {"3s__wangfu_choice1", "3s__wangfu_choice2"}
    local choices = table.simpleClone(all_choices)
    if not to:isWounded() then table.remove(choices, 1) end
    local choice = room:askToChoice(to, {
      choices = choices,
      skill_name = skill.name,
      prompt = "#3s__wangfu-choose",
      all_choices = all_choices,
    })
    room:sendLog{
      type = "#wangfu_choice",
      from = to.id,
      arg = choice,
      toast = true,
    }
    local choice_num = table.indexOf(all_choices, choice)
    -- 检查是否可以横置改变选择
    local can_chain = not to.chained and
    room:askToSkillInvoke(player, {
      skill_name = skill.name,
      prompt = "#3s__wangfu-chain::"..to.id,
    })
    if can_chain then
      to:setChainState(true)
      -- 改为执行另一项
      choice_num = 3 - choice_num -- 1变2，2变1
      choice = all_choices[choice_num]
    end
    -- 执行选择的效果
    if choice_num == 1 then
      -- 回复1点体力，多结算一次
      room:recover{
        who = to,
        num = 1,
        skillName = skill.name,
      }
      -- 设置多结算一次
      data.additionalEffect = (data.additionalEffect or 0) + 1
    else
      -- 失去1点体力，少结算一次
      room:loseHp(to, 1, skill.name)
      -- 设置少结算一次
      if to.dead then return end
      data.additionalEffect = (data.additionalEffect or 0) - 1
      if data.additionalEffect < 0 then
        data:removeAllTargets()
      end
    end
  end,
})

return skill